package delay;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author if
 * @Description: What is it
 * @Date 2021-10-25 下午 05:29
 */
public class DelayProducer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer producer = new DefaultMQProducer("pg");
        producer.setNamesrvAddr("centos:9876");
        producer.start();

        for(int i=0;i<100;i++){
            byte[] body=("Hi,"+i).getBytes();
            Message msg=new Message("DelayTopic","DelayTag",body);
            //指定消息延迟等级为3，即10s
            msg.setDelayTimeLevel(3);
            SendResult sendResult = producer.send(msg);
            //输出消息发送的时间
            System.out.println(new SimpleDateFormat("mm:ss").format(new Date()));
            //输出消息返回结果
            System.out.println(" , "+sendResult);
        }
        producer.shutdown();
    }
}
